package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.DepartmentsHanger;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.service.DepartmentsHangerService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author ht
 */
@Component
public class DepartmentsHangerDataReader extends ExcelReader<DepartmentsHanger> {

    @Autowired
    private DepartmentsHangerService departmentsHangerService;

    @Override
    protected String saveData(List<DepartmentsHanger> dataList, Long batchId) {
        EntityWrapper<DepartmentsHanger> wrapper = new EntityWrapper<DepartmentsHanger>();
        wrapper.eq("batch_id", batchId);
        deletOrderFile(batchId);
        departmentsHangerService.delete(wrapper);//删除之前导入的数据
        for (DepartmentsHanger departmentsHanger : dataList) {
            departmentsHanger.setBatchId(batchId);
            departmentsHanger.setCreateTime(new Date());
            departmentsHanger.setOperatorId(getCurrentUserId());
        }
        departmentsHangerService.insertBatch(dataList, 100);
        return "";
    }

    private void deletOrderFile(Long batchId) {
        System.out.println(1);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    @Override
    protected List<DepartmentsHanger> verifyData(HSSFWorkbook workbook) {
        List<DepartmentsHanger> departmentsHangers = new ArrayList<DepartmentsHanger>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        DepartmentsHanger departmentsHanger;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            departmentsHanger = new DepartmentsHanger();
            departmentsHanger.setName(getStringCellValue(sheet.getRow(i), 0));
            departmentsHanger.setWard(getIntCellValue(sheet.getRow(i), 1));
            departmentsHanger.setOutpatient(getIntCellValue(sheet.getRow(i), 2));
            departmentsHanger.setOperate(getIntCellValue(sheet.getRow(i), 3));
            departmentsHangers.add(departmentsHanger);
        }
        return departmentsHangers;
    }
    @Override
    protected List<DepartmentsHanger> verifyData7(XSSFWorkbook workbook) {
        return null;
    }


}
